﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wolfy.NetDisk.Core.Model;
using Wolfy.NetDisk.Core.Security;
using Wolfy.NetDisk.Core.Service;

namespace Wolfy.NetDisk.Site.Controllers
{
    /// <summary>
    ///系统初始化设置
    /// </summary>
    public class SettingController : Controller
    {
        private readonly IUserService userService;
        private readonly IDepartmentService departmentService;
        private readonly IRoleService roleService;
        private readonly IIconService iconService;
        private readonly INavigationService navigationService;
        private readonly IFolderService folderService;

        public SettingController(IUserService userService, IDepartmentService departmentService, IRoleService roleService, IIconService iconService, INavigationService navigationService, IFolderService folderService)
        {
            this.userService = userService;
            this.departmentService = departmentService;
            this.roleService = roleService;
            this.iconService = iconService;
            this.navigationService = navigationService;
            this.folderService = folderService;
        }
        // GET: Setting
        public ActionResult Init()
        {
            //初始化角色
            int userRoleId = roleService.Create(new Role { IsDeleted = false, Name = "普通用户", Users = null });
            int adminRoleId = roleService.Create(new Role { IsDeleted = false, Name = "管理员", Users = null });

            //初始化部门
            int id = departmentService.Create(new Department { IsDeleted = false, Name = "北京wolfy云有限公司", Users = null, ParentDepartment = null });

            var parent = departmentService.Get(id);
            departmentService.Create(new Department { IsDeleted = false, ParentDepartment = parent, Dt = DateTime.Now, Name = "人力资源", Users = null });
            departmentService.Create(new Department { IsDeleted = false, ParentDepartment = parent, Dt = DateTime.Now, Name = "财务", Users = null });
            departmentService.Create(new Department { IsDeleted = false, ParentDepartment = parent, Dt = DateTime.Now, Name = ".Net开发部", Users = null });

            var admin = roleService.Get(adminRoleId);
            userService.Create(new User { UserName = "admin", Department = parent, Dt = DateTime.Now, Gender = Gender.F, IsDeleted = false, Pwd = AesSecret.EncryptToken("123456"), Roles = new List<Role> { admin } });

            var user = roleService.Get(userRoleId);
            //用户网盘 菜单
            int parentNavId = navigationService.Create(new Navigation { Dt = DateTime.Now, Icon = "/Content/Images/main.png", IsDeleted = false, Name = "全部文件", Nav = null, Role = user, Url = "/netdisk/home" });
            var parentNav = navigationService.Get(parentNavId);
            navigationService.Create(new Navigation { Dt = DateTime.Now, Icon = "/Content/Images/FileIcon/ImgType.png", IsDeleted = false, Name = "图片", Nav = parentNav, Role = user, Url = "/netdisk/home/images" });
            navigationService.Create(new Navigation { Dt = DateTime.Now, Icon = "/Content/Images/FileIcon/XlsType.png", IsDeleted = false, Name = "文档", Nav = parentNav, Role = user, Url = "/netdisk/home/docs" });
            navigationService.Create(new Navigation { Dt = DateTime.Now, Icon = "/Content/Images/FileIcon/VideoType.png", IsDeleted = false, Name = "视频", Nav = parentNav, Role = user, Url = "/netdisk/home/videos" });
            navigationService.Create(new Navigation { Dt = DateTime.Now, Icon = "/Content/Images/FileIcon/TorrentType.png", IsDeleted = false, Name = "种子", Nav = parentNav, Role = user, Url = "/netdisk/home/bits" });
            navigationService.Create(new Navigation { Dt = DateTime.Now, Icon = "/Content/Images/FileIcon/MusicType.png", IsDeleted = false, Name = "音乐", Nav = parentNav, Role = user, Url = "/netdisk/home/musics" });
            navigationService.Create(new Navigation { Dt = DateTime.Now, Icon = "/Content/Images/FileIcon/OtherType.png", IsDeleted = false, Name = "其它", Nav = parentNav, Role = user, Url = "/netdisk/home/other" });
            navigationService.Create(new Navigation { Dt = DateTime.Now, Icon = "/Content/Images/FileIcon/share.png", IsDeleted = false, Name = "我的分享", Nav = null, Role = user, Url = "/netdisk/home/myshare" });
            navigationService.Create(new Navigation { Dt = DateTime.Now, Icon = "/Content/Images/FileIcon/recyclebin.png", IsDeleted = false, Name = "回收站", Nav = null, Role = user, Url = "/netdisk/home/recyclebin" });
            ViewBag.msg = "初始化成功";
            //管理员菜单
            int adminNavId = navigationService.Create(new Navigation { Dt = DateTime.Now, Icon = "/Content/Images/main.png", IsDeleted = false, Name = "系统管理", Nav = null, Role = admin, Url = "/manager/home" });
            var parentAdminNav = navigationService.Get(adminNavId);
            navigationService.Create(new Navigation { Dt = DateTime.Now, Icon = "/Content/Images/person.png", IsDeleted = false, Name = "用户管理", Nav = parentAdminNav, Role = admin, Url = "/manager/users" });
            navigationService.Create(new Navigation { Dt = DateTime.Now, Icon = "/Content/Images/main.png", IsDeleted = false, Name = "部门管理", Nav = parentAdminNav, Role = admin, Url = "/manager/departments" });
            navigationService.Create(new Navigation { Dt = DateTime.Now, Icon = "/Content/Images/FileIcon/CADType.png", IsDeleted = false, Name = "文件类型", Nav = parentAdminNav, Role = admin, Url = "/manager/files" });
            navigationService.Create(new Navigation { Dt = DateTime.Now, Icon = "/Content/Images/log.png", IsDeleted = false, Name = "系统日志", Nav = parentAdminNav, Role = admin, Url = "/manager/logs" });
            int folderId = iconService.Create(new Icon { Ext = string.Empty, Dt = DateTime.Now, IsDeleted = false, Path = "/Content/Images/FileIcon/FolderType.png" });
            var folder = iconService.Get(folderId);
            int pngId = iconService.Create(new Icon { Ext = ".png", Dt = DateTime.Now, IsDeleted = false, Path = "/Content/Images/FileIcon/ImgType.png" });
            var png = iconService.Get(pngId);
            int jpgId = iconService.Create(new Icon { Ext = ".jpg", Dt = DateTime.Now, IsDeleted = false, Path = "/Content/Images/FileIcon/ImgType.png" });
            var jpg = iconService.Get(jpgId);
            int gifId = iconService.Create(new Icon { Ext = ".gif", Dt = DateTime.Now, IsDeleted = false, Path = "/Content/Images/FileIcon/ImgType.png" });
            var gif = iconService.Get(gifId);
            int otherId = iconService.Create(new Icon { Ext = ".other", Dt = DateTime.Now, IsDeleted = false, Path = "/Content/Images/FileIcon/OtherType.png" });
            var other = iconService.Get(otherId);

            return View();
        }
    }
}